/**----------------------------------------------------------*/
/** JS:  cmi_admin_log.js（权限管理--管理员日志）             */
/**----------------------------------------------------------*/
var tipc = "ADMIN_LOG";

$(function(){
	try {
		loadMemberPanel();
		loadIpAddress();
	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var loadIpAddress = function() {
	MyUtils.sendPost("impl_search/searchObjectList.ajax",{qy:"[]",st:tipc},function(msg){
		try{
			var rest = MyUtils.toJsonObject(msg);
			if(rest.length > 0) {
				var opt='';
				for(var i = 0; i < rest.length; i++) {
					opt += '<option value="'+rest[i].ip+'">'+rest[i].ip+'</option>';
				}
				$('#ipSearch').append(opt);
			}
		} catch (e) {
		}
	});
};

var loadMemberPanel = function() {
	var columns = [  {field : 'uuid',title : '编号',width : 100,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'adminId',title : '操作者',width : 150,sortable:true,sorter:function(a,b){alert(a + "  " + b);},align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'logtime',title : '操作时间',width : 150,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'ip',title : 'IP地址',width : 180,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'loginfo',title : '操作记录',width : 250,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
				];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.desc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,null,null,panel,null);
	$('#dataPanel').datagrid({
		title:null,
		iconCls:null,
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
	
	$('#search').linkbutton({
		plain : true
	}).click(function(){
		var selected = $('#ipSearch').val();
		var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.desc};
		if(selected !=  '0') {
			pam = {qr:"["+MyUtils.createQueryConditionBean("ip","=","?", selected,null) +"]",st:tipc,sco:'',dc:Dmlx.sort.desc};
		} 
		MyUtils._searchPanelData('dataPanel', 'impl_search/commonsPaginSearch.ajax', pam);
	});
	
	if(toolbars != null && toolbars.length > 0) {
		$('#remove-log').show();
		
		$('#clearLog').linkbutton({
			plain : true
		}).click(function(){
			var selected = $('#removeTime').val();
			if(selected != 0) {
				var qy = "";
				var msg = "";
				if(selected == 1) {
					msg = "[一周之前]";
					qy = MyUtils.createQueryConditionBean("0","customer","DATE_FORMAT(logtime,'%Y-%m-%d') <= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)","0",null);
				} else if (selected == 2) {
					msg = "[一个月之前]";
					qy = MyUtils.createQueryConditionBean("0","customer","DATE_FORMAT(logtime,'%Y-%m-%d') <= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)","0",null);
				} else if (selected == 3) {
					msg = "[三个月之前]";
					qy = MyUtils.createQueryConditionBean("0","customer","DATE_FORMAT(logtime,'%Y-%m-%d') <= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)","0",null);
				} else if (selected == 4) {
					msg = "[半年之前]";
					qy = MyUtils.createQueryConditionBean("0","customer","DATE_FORMAT(logtime,'%Y-%m-%d') <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)","0",null);
				} else if (selected == 5) {
					msg = "[一年之前]";
					qy = MyUtils.createQueryConditionBean("0","customer","DATE_FORMAT(logtime,'%Y-%m-%d') <= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)","0",null);
				}
				if(qy !='') {
					var data = { data:"["+qy +"]"
						    ,st:tipc
						    ,msg:msg
						   };
					removeLog(data);
				}
			}
		});
		
		$('#clearLog_select').linkbutton({
			plain : true
		}).click(function(){
			var rows = $('#dataPanel').datagrid("getSelections");
			if(rows.length == 0) {
				MyUtils.waring(_empty_select);
				return;
			}
			var ids = new Array();
			for(var i = 0; i < rows.length; i++) {
				ids.push(rows[i].uuid);
			}
			var data = { data:"["+MyUtils.createQueryConditionBean("uuid","customer","uuid IN (" + ids.toString() + ")","0",null) +"]"
					    ,st:tipc
					    ,msg:"选中[" + ids.length +"]条"
					   };
			
			removeLog(data);
		});
	}
};

var removeLog = function(data) {
	try {
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(_remove_confirm,"left"), function(r) {
			if (r) {

				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
	} catch (e) {
		MyUtils.hiddenMask($('#t2'));
		showJsException(e,"数据加载失败!");
	}
};